package com.woniuxy.woniufp.bank.controller;

import com.woniuxy.woniufp.bank.service.ITradeLogService;
import com.woniuxy.woniufp.common.model.constant.MQConstant;
import com.woniuxy.woniufp.common.model.dto.bank.BankCardAddDTO;
import com.woniuxy.woniufp.common.model.dto.bank.BankCardRequestDto;
import com.woniuxy.woniufp.common.model.dto.bank.SignatureDTO;
import com.woniuxy.woniufp.common.model.result.Result;
import com.woniuxy.woniufp.common.model.vo.trade.BorrowSyncRecordVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@Api(tags = "交易日志")
public class TradeLogController {
    @Autowired
    ITradeLogService tradeLogService ;

    @Autowired
    RabbitTemplate rabbitTemplate ;
    /**
     * 绑卡生成交易记录
     */
    @PostMapping("/bank/access/card")
    @ApiOperation("生成用户绑定日志")
    public SignatureDTO proxyBindCardLog(@RequestBody BankCardAddDTO bankCardAddDTO) {
        log.info("接收到的用户请求绑卡的数据(BankCardAddDTO):{}",bankCardAddDTO);
        SignatureDTO signatureDto = tradeLogService.proxyBindCardLog(bankCardAddDTO);
        return signatureDto;
    }


    /**
     * 银行托管系统绑卡回调结果
     * 回调地址：http://localhost:5500/bank/access/receive
     */
    @ApiOperation("银行托管系统绑卡回调结果")
    @PostMapping(value = "/bank/access/receive")
    public String callbackUpdate(@RequestBody BankCardRequestDto bankCardRequestDto){
        //1. 修改托管系统交易状态为已同步
        tradeLogService.modifyRequestStatus(bankCardRequestDto.getReqCode(),1);

        //2.发送消息给用户中心 （更新用户信息-绑卡状态）（银行卡编码、开户行名称）
        rabbitTemplate.convertAndSend(MQConstant.WONIUFP_EXCHANGE_ACCOUNT,MQConstant.WONIUFP_ACCOUNT_ROUTING,bankCardRequestDto);
        //3.返回结果
        return "OK";
    }
    /**
     * 借款记录审核，同步借款数据到银行托管系统
     */
    @PostMapping("/bank/access/sync/borrow")
    @ApiOperation("借款记录审核，同步数据")
    public Result syncBorrowRecord(@RequestBody BorrowSyncRecordVo borrowSyncRecordVo){
        tradeLogService.syncBorrowRecord(borrowSyncRecordVo);
        return Result.ok();
    }



}
